package com.ruoyi.web.controller.purchase;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.purchase.domain.PoBrand;
import com.ruoyi.purchase.domain.PoFirm;
import com.ruoyi.purchase.domain.PoProduct;
import com.ruoyi.purchase.domain.PoType;
import com.ruoyi.purchase.service.PoBrandService;
import com.ruoyi.purchase.service.PoFirmService;
import com.ruoyi.purchase.service.PoProductService;
import com.ruoyi.purchase.service.PoTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 商品管理 Controller
 */
@Controller
@RequestMapping("/product/manage")
public class ProductManageController extends BaseController {
    private String prefix="product/manage/";

    @Autowired
    private PoProductService poProductService;
    @Autowired
    private PoBrandService poBrandService;
    @Autowired
    private PoTypeService poTypeService;
    @Autowired
    private PoFirmService poFirmService;

    @RequiresPermissions("product:manage:view")
    @RequestMapping()
    public String proManage(Model model){
        System.out.println(poBrandService);
        List<PoBrand> brandList = poBrandService.list();
        model.addAttribute("brandList",brandList);
        List<PoType> proTypeList = poTypeService.list();
        model.addAttribute("proTypeList",proTypeList);
        return prefix+"proManage";
    }

    @RequiresPermissions("product:manage:list")
    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(PoProduct poProduct){
        startPage();
        List<PoProduct> proManageList= poProductService.findProManageList(poProduct);
        return getDataTable(proManageList);
    }

    @RequestMapping("/add")
    public String add(Model model){
        List<PoBrand> brandList = poBrandService.list();
        model.addAttribute("brandList",brandList);
        List<PoType> proTypeList = poTypeService.list();
        model.addAttribute("proTypeList",proTypeList);
        List<PoFirm> firmList = poFirmService.findFirmList();
        model.addAttribute("firmList",firmList);
        model.addAttribute("cUser",getSysUser());
        return prefix+"add";
    }

    @RequiresPermissions("product:manage:add")
    @Log(title = "商品管理",businessType = BusinessType.INSERT)
    @RequestMapping("/doAdd")
    @ResponseBody
    public AjaxResult addSave(PoProduct poProduct){
        return toAjax(poProductService.save(poProduct));
    }

    @RequestMapping("/edit/{proId}")
    public String edit(@PathVariable("proId") Integer productId,Model model){
        PoProduct poProduct = poProductService.getById(productId);
        model.addAttribute("poProduct",poProduct);
        List<PoBrand> brandList = poBrandService.list();
        model.addAttribute("brandList",brandList);
        List<PoType> proTypeList = poTypeService.list();
        model.addAttribute("proTypeList",proTypeList);
        List<PoFirm> firmList = poFirmService.findFirmList();
        PoProduct editPro = poProductService.findEditPro(productId);
        model.addAttribute("editPro",editPro);
        model.addAttribute("cUser",getSysUser());

        return prefix+"edit";
    }

    @Log(title = "品牌修改", businessType = BusinessType.UPDATE)
    @RequestMapping("/doEdit")
    @ResponseBody
    public AjaxResult editSave(@Validated PoProduct poProduct){
        return toAjax(poProductService.updateById(poProduct));
    }

    @RequiresPermissions("product:manage:remove")
    @Log(title = "品牌删除", businessType = BusinessType.DELETE)
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Integer ids){
        return toAjax(poProductService.removeById(ids));
    }


}
